﻿/**
 * SVNAddTemplateViewModel
 * 视图模型绑定器
 * @Author:ZuiHong
 * @Date:2018/08/29 10:09
 **/
namespace metro_wpf.ViewModel
{
    using System;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Windows.Input;

    public class SVNAddTemplateViewModel : INotifyPropertyChanged,IDataErrorInfo,IDisposable
    {
        private ICommand textBoxButtonCommand;

        private ICommand childButtonCommand;

        private string parentProjectPath;

        private string childProjectPath;

        private bool isChildProjectEnabled = false;

        public event PropertyChangedEventHandler PropertyChanged;

        #region IDataErrorInfo Interface  TODO
        public string this[string columnName] => throw new NotImplementedException();

        public string Error => throw new NotImplementedException();
        #endregion

        /// <summary>
        /// 父项目文件地址
        /// </summary>
        public string ParentProjectPath
        {
            get
            {
                return parentProjectPath;
            }
            set
            {
                if (value.Equals(ParentProjectPath)) return;
                parentProjectPath = value;
                RaisePropertyChanged("ParentProjectPath");
            }
        }

        /// <summary>
        /// 子项目文件地址
        /// </summary>
        public string ChildProjectPath
        {
            get{ return childProjectPath; }
            set
            {
                if (value.Equals(ChildProjectPath)) return;
                ChildProjectPath = value;
                RaisePropertyChanged("ChildProjectPath");
            }
        }

        /// <summary>
        /// 启用添加子项目文件夹键
        /// </summary>
        public bool IsChildProjectEnabled
        {
            get{ return isChildProjectEnabled; }
            set
            {
                if (value.Equals(IsChildProjectEnabled)) return;
                isChildProjectEnabled = value;
                RaisePropertyChanged("IsChildProjectEnabled");
            }
        }

        /// <summary>
        /// 父文件夹选择
        /// </summary>
        public ICommand ParentDialog
        {
            get
            {
                return this.textBoxButtonCommand ?? (this.textBoxButtonCommand = new SimpleCommand
                {
                    CanExecuteDelegate = x => true,
                    ExecuteDelegate = x =>
                    {
                        FolderBrowserDialog dialog = new FolderBrowserDialog();
                        dialog.ShowNewFolderButton = false;

                        if (dialog.ShowDialog() == DialogResult.OK)
                        {
                            string[] paths = dialog.SelectedPath.Split('\\');
                            ParentProjectPath = paths[paths.Length - 1];
                            if (ParentDialog != null) IsChildProjectEnabled = true;
                            else IsChildProjectEnabled = false;
                        }
                    }
                });
            }
        }

        /// <summary>
        /// 子文件夹选择
        /// </summary>
        public ICommand ChildDialog
        {
            get
            {
                return this.childButtonCommand ?? (this.childButtonCommand = new SimpleCommand
                {
                    CanExecuteDelegate = x => true,
                    ExecuteDelegate = x =>
                    {
                        FolderBrowserDialog dialog = new FolderBrowserDialog();
                        if (dialog.ShowDialog() == DialogResult.OK)
                        {
                            
                        }
                    }
                });
            }
        }

        

        protected virtual void RaisePropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public void Dispose()
        {
            GC.Collect();
        }

    }
}
